package it.tukano.treditor.modules;

import it.tukano.app.AbstractAppModule;
import it.tukano.app.AppModule;
import it.tukano.check.Todo;
import it.tukano.reflection.MethodExt;
import it.tukano.swingdeco.containers.JPanelExt;
import it.tukano.swingdeco.containers.JRowPanel;
import it.tukano.treditor.engine.scene.DirectionalLightElement;
import it.tukano.treditor.engine.scene.SceneElement;
import it.tukano.treditor.modules.datamodel.DataModelEvent;
import it.tukano.treditor.modules.datamodel.DataModelListener;
import it.tukano.treditor.resources.Labels;
import it.tukano.treditor.resources.Labels.LabelKey;
import it.tukano.treditor.ui.ColorEditor;
import it.tukano.treditor.ui.Tuple3nEditor;
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JScrollPane;

public class DirectionalLightEditor extends AbstractAppModule implements DataModelListener {
    private JPanelExt form;
    private DirectionalLightElement editedLight;
    private Tuple3nEditor directionEditor;
    private ColorEditor colorEditor;

    /**
     * Instance initializer
     */
    public DirectionalLightEditor() {
    }

    @Override
    public void startModule(AppModule context) {
        MethodExt listener = new MethodExt(this, "editorEventPerformed");
        Labels labels = Labels.getInstance();
        String dirx = labels.getLabelValue(LabelKey.DIRX);
        String diry = labels.getLabelValue(LabelKey.DIRY);
        String dirz = labels.getLabelValue(LabelKey.DIRZ);
        directionEditor = new Tuple3nEditor(dirx, diry, dirz, 0.1);
        directionEditor.setTitledBorder(labels.getLabelValue(LabelKey.DIRECTION));
        colorEditor = new ColorEditor();
        colorEditor.setTitledBorder(labels.getLabelValue(LabelKey.LIGHT_COLOR));
        colorEditor.withPreferredSize(100, 50);
        directionEditor.addListener(listener);
        colorEditor.addListener(listener);
        JRowPanel rows = JRowPanel.newTopAlignedInstance()
                .addRow(directionEditor)
                .addRow(colorEditor);
        form = new JPanelExt(new BorderLayout())
                .append(new JScrollPane(rows))
                .withName(labels.getLabelValue(LabelKey.DIRECTIONAL_LIGHT_SETTINGS));
        find(DataModel.class).addListener(this);
    }

    public void dataModelEventPerformed(DataModelEvent e) {
        if(e.getEventSource() == this) return;
        
        switch(e.getType()) {
            case SELECTED_ELEMENT_SET:
                handleSelectedElementSet(e);
                break;
            case DIRECTIONAL_LIGHT_UPDATE:
                Todo.implementThis();
                break;
        }
    }
    
    public void editorEventPerformed() {
        editedLight.setColor(colorEditor.getEditedValue());
        editedLight.setDirection(directionEditor.getEditedValue());
        find(DataModel.class).notifyDirectionalLightUpdated(this, editedLight);
    }

    private void handleSelectedElementSet(DataModelEvent e) {
        if(e.getArgumentCount() == 0) {
            hideFormIfVisible();
        } else {
            SceneElement selectedElement = e.getArg(0, SceneElement.class);
            if(selectedElement instanceof DirectionalLightElement) {
                setEditedLight((DirectionalLightElement) selectedElement);
                showForm();
            } else {
                hideFormIfVisible();
            }
        }
    }

    private void hideFormIfVisible() {
        if(form.isShowing()) {
            find(MainFrame.class).uninstall(form);
        }
    }

    private void setEditedLight(DirectionalLightElement directionalLightElement) {
        this.editedLight = directionalLightElement;
        directionEditor.setEditedValue(editedLight.getDirection());
        colorEditor.setEditedValue(editedLight.getColor());
    }

    private void showForm() {
        if(!form.isShowing()) {
            find(MainFrame.class).install(form, BorderLayout.EAST, true);
        }
    }
}
